package com.example.selflearning.service;

import com.alicp.jetcache.anno.Cached;
import com.example.selflearning.domain.TestModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;
/**
 * Created by yangjingzhi on 2019/5/15.
 */
@Slf4j
@Component
public class SpringCacheServiceImpl implements SpringCacheService {

    /*@CachePut(value = "models", key = "#testModel.name")*/
    @Cacheable(value = "models"  ,key = "#testModel.name")
    public TestModel getTestModel(TestModel testModel){
        log.info("come in //.......................");
        //TestModel testModel= new TestModel();
        testModel.setName(testModel.getName().toUpperCase());
        return testModel;
    }

    @Cacheable(value = "models"  ,key = "#testModel.name")
    public TestModel saveTestModel(TestModel  testModel){
        log.info("put in //.......................");
        //TestModel testModel= new TestModel();
        testModel.setName(testModel.getName().toUpperCase());
        return testModel;
    }

    @CacheEvict(value="models",key = "#batch.name")
    public void delTestModel(TestModel batch){
        log.info("del in //.......................");
    }
    //beforeInvocation=true
    @CacheEvict(value="models" , allEntries=true)
    public void delAllTestModel(){
        log.info("del all in //.......................");
    }


    @Cached(name="MyJetCache",expire=30,timeUnit= TimeUnit.MINUTES)
    public String getJetCache(String key){
        log.info("JetCache in //.......................");
        return key+" value is "+key +"+10";
    }

}
